package cc.shacocloud.redis.study.queue.delay.example;

import cc.shacocloud.redis.study.queue.delay.DelayMessage;
import cc.shacocloud.redis.study.queue.delay.RedisDelayMessage;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 延时队列示例
 */
@Slf4j
public class DelayMessageExample extends RedisDelayMessage {

    public DelayMessageExample() {
        super("dme");
    }

    @Override
    public void consume(long expire, byte[] data) throws Exception {
        log.debug("从redis中消费延时消息 '{}' ，到期时间为：{}，消费时间：{}",
                new String(data), DateUtil.date(expire).toMsStr(), DateUtil.date().toMsStr());
    }


    public static void main(String[] args) {
        DelayMessage delayMessage = new DelayMessageExample();

        for (int i = 0; i < 10; i++) {
            int delay = RandomUtil.randomInt(500, 10000);
            String data = RandomUtil.randomString(10);

            // 发布延时消息
            long expire = delayMessage.delay(data.getBytes(), delay);
            log.debug("发布延时消息 '{}' 至redis中，到期时间为：{}", data, DateUtil.date(expire).toMsStr());
        }
    }
}
